Skip to content

[REBASE & FF] Finish Unmapping Free Heap Pages - #1916

Merged
os-d merged 7 commits into
OpenDevicePartnership:mainfrom
os-d:drop_linked_list_allocator
Nov 4, 2026
Merged

os-d merged 7 commits into
OpenDevicePartnership:mainfrom
os-d:drop_linked_list_allocator

Conversation

@os-d

@os-d os-d commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

This PR is the completion of the work started in #1837. It removes the linked list allocator crate in favor of direct GCD allocations when the fixed size blocks cannot satisfy a request ensures that all heap pages are unmapped when not in use (allocated by callers or used as metadata pages).

Closes #603 and #1846.

There are five commits:

EventDB: Defer Memory Map and Cache Attribute Change Events

Today, the fixed size block allocator and the GCD have a circular dependency. The FSB allocates pages from the GCD to fill its fixed block lists and directly sends page allocations to the GCD. The GCD does not allocate its descs from pool backed memory, but does fire the memory map and cache attribute change events, which go into the event DB, which does allocate pool memory. This causes odd locking behavior to be required in the FSB to drop the lock (which also lowers TPL back down to the caller's level, processing pending events), call into the GCD (acquiring its lock, raising TPL to HIGH, then dropping its lock, lowering TPL, processing events), then acquiring the FSB lock again (going to TPL HIGH, then lowering and processing events).

Outside of these two events, the GCD does not do any pool allocations. The strange locking behavior leaves gaps for allocs/frees to be incomplete when other events are being processed, which can lead to a bad state, particularly in upcoming changes to the allocator structure. It also has a negative performance impact to do the TPL manipulations and subsequent event processing many times.

This commit adds special handling for the memory map and cache attribute change event, preventing memory allocation and deferring their execution until the next TPL lowering, when we process pending events anyways. This will allow a future commit in this series to change the FSB locking to remain at TPL_HIGH_LEVEL and holding the FSB lock for the duration of an alloc/free, removing the perf concern and the reliability gaps.

These events are safe to defer and in fact it is better to, further memory map modifications may occur (which may also change cache attributes) and so instead of firing the event multiple times, we fire a single time when the operation is complete.

GCD: Cleanup Cache Event Sending

The logic of when to send a cache event and call the cacheability change API is a bit hard to read, this cleans it up.

GCD: Check Allocation Suitability Before Splitting Block

The GCD currently finds a candidate memory block for an allocation, splits the block, does list maintenance, checks if the block is allocatable, then if it is not, it remerges the block, does list maintenance, and returns an error, telling the GCD to try the next block.

This has been observed to have a large performance impact due to this being on the memory allocation hotpath. By moving the
logic to check if the block is allocatable before doing the split, a physical Intel platform was seen to boot >200ms faster. Before this change, on the platform tested, each GCD allocation was observed to have an average of 54 blocks split and remerged
before it found a block that was actually allocatable. This equated to 2.8 billion ticks spent doing splits and merges vs 200 million ticks spent doing splits and merges after the change.

This commit changes the bottom up and top down allocations to check if the block is allocatable before splitting it.

Allocator: Unmap Non-FSB Heap Pages and Drop Allocator Crate

Currently, we use the linked list allocator crate to manage allocations that don't hit our fixed size blocks. However, our use of the crate is fairly minimal and it prevents us from unmapping the pages that the allocator owns, which is contrary to our use after free catching goals.

This commit does a large refactor to the allocator system to stop using the linked list allocator crate and instead back all heap allocations greater than our largest FSB with pages directly. It then updates the management of retired and reclaimed pages to account for the new management. To keep this management simple, when a page is reclaimed, it is eagerly split into one block size.

Following this change, all unused heap pages are unmapped, except for the pages that keep metadata on this heap region. Heap pages are retired and reclaimed in FIFO order (as are FSBs) to have the longest time between using a page and it getting used again and increasing the change that page will be retired and catch any use after free.

FSB: Remove Page Accounting Node

An earlier refactor the FSB introduced the page accounting node as a last used cache for heap owned regions as a potential performance improvement. However, the performance of just this piece was not tested independently.

When testing independently on physical HW, it was not observed to add any performance benefit, so it is dropped.

  • Impacts functionality?
  • Impacts security?
  • Breaking change?
  • Includes tests?
  • Includes documentation?

How This Was Tested

Tested on Q35 and ArmVirt as well as physical Intel and physical Arm. Performance testing was done on the two physical machines and no significant performance delta was observed with these changes (as an entire changeset).

Integration Instructions

Use-after-free has a higher chance of getting caught, so page faults might be seen after these changes. Drivers accessing free memory need to be fixed.

@patina-automation

patina-automation Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

✅ QEMU Validation Passed

All QEMU validation jobs completed successfully.

Note: Q35 is only built on Windows hosts (QEMU boot is disabled due to a QEMU vfat issue).

Workflow run: https://github.com/OpenDevicePartnership/patina/actions/runs/41891403071

Boot Time to EFI Shell

Platform Elapsed
ArmVirt (Linux Host) 24.5s
Q35 (Linux Host) 24.8s

Dependencies

Repository Ref
patina 847b6ad
patina-dxe-core-qemu 7e29a6f
patina-fw-patcher 820b0ae
patina-qemu firmware v4.1.4
patina-qemu build script 5ab2044

Binary Size Report

Platform Commit Δ Debug Δ Release Δ Debug
(LZMA)
Δ Release
(LZMA)
Q35 (x86_64) Base:cdd5f45
PR:847b6ad
 
1.23 MB
1.24 MB
Δ +7.50 KB
979.00 KB
991.00 KB
Δ +12.00 KB
473.64 KB
476.63 KB
Δ +3.00 KB
384.87 KB
389.22 KB
Δ +4.36 KB
ArmVirt (aarch64) Base:cdd5f45
PR:847b6ad
 
1.12 MB
1.12 MB
Δ +512.00 B
895.00 KB
904.00 KB
Δ +9.00 KB
386.39 KB
388.14 KB
Δ +1.75 KB
316.07 KB
319.21 KB
Δ +3.15 KB

Compressed sizes use LZMA-alone format at preset 6 (xz --format=lzma -6).

Shared QEMU integration: 7e29a6f.

This comment was automatically generated by the Patina QEMU PR Validation Post workflow.

@github-actions github-actions Bot added the impact:security Has a security impact label Oct 23, 2026
@os-d os-d linked an issue Oct 23, 2026 that may be closed by this pull request
@codecov

codecov Bot commented Oct 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.96907% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
patina_dxe_core/src/gcd/memory_block.rs 97.87% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@os-d

os-d commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

I did do some profiling for the large pool allocations and on Q35, there were ~1,300 allocations >4K <8k and ~60 allocations >8k. I did a perf test on Intel HW with extending the FSBs to 8k. There was no appreciable perf difference in doing so and there was additional overhead since the retired page tracking assumes a max of 1 page at a time, so I dropped that code.

Comment thread patina_dxe_core/src/allocator.rs Resolved
Comment thread patina_dxe_core/src/gcd/memory_block.rs Resolved
@os-d
os-d force-pushed the drop_linked_list_allocator branch from 609252b to 126b649 Compare October 28, 2026 19:29
@os-d

os-d commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

@makubacki I have addressed feedback, please re-review.

@cfernald cfernald left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should try further improvements such as lazily splitting the FSB pages, but this change is already pretty large so I don't think now is the time.

@makubacki

Copy link
Copy Markdown
Collaborator

@makubacki I have addressed feedback, please re-review.

Done

@os-d
os-d force-pushed the drop_linked_list_allocator branch from 126b649 to 9de42b2 Compare October 30, 2026 21:22
@os-d

os-d commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

@cfernald @makubacki @joschock I have pushed one more commit to address the issue Chris raised that ARM64 runtime allocators could end up with very large allocations for large heap allocations. This alleviates that problem by searching retired pages for allocations first before doing a direct GCD allocation. Please re-review. The earlier commits are unchanged. I am getting a new run of perf data on both x64 and arm64 with this.

os-d added 6 commits November 4, 2026 10:10
Today, the fixed size block allocator and the GCD have a
circular dependency. The FSB allocates pages from the GCD
to fill its fixed block lists and directly sends page allocations
to the GCD. The GCD does not allocate its descs from pool backed
memory, but does fire the memory map and cache attribute change
events, which go into the event DB, which does allocate pool
memory. This causes odd locking behavior to be required in
the FSB to drop the lock (which also lowers TPL back down to
the caller's level, processing pending events), call into the
GCD (acquiring its lock, raising TPL to HIGH, then dropping its
lock, lowering TPL, processing events), then acquiring the FSB
lock again (going to TPL HIGH, then lowering and processing events).

Outside of these two events, the GCD does not do any pool allocations.
The strange locking behavior leaves gaps for allocs/frees to be
incomplete when other events are being processed, which can lead
to a bad state, particularly in upcoming changes to the allocator
structure. It also has a negative performance impact to do the
TPL manipulations and subsequent event processing many times.

This commit adds special handling for the memory map and cache
attribute change event, preventing memory allocation and deferring
their execution until the next TPL lowering, when we process pending
events anyways. This will allow a future commit in this series to
change the FSB locking to remain at TPL_HIGH_LEVEL and holding the
FSB lock for the duration of an alloc/free, removing the perf
concern and the reliability gaps.

These events are safe to defer and in fact it is better to, further
memory map modifications may occur (which may also change cache
attributes) and so instead of firing the event multiple times,
we fire a single time when the operation is complete.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
The logic of when to send a cache event and call the
cacheability change API is a bit hard to read, this
cleans it up.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
The GCD currently finds a candidate memory block for an
allocation, splits the block, does list maintenance, checks
if the block is allocatable, then if it is not, it remerges
the block, does list maintenance, and returns an error, telling
the GCD to try the next block.

This has been observed to have a large performance impact due
to this being on the memory allocation hotpath. By moving the
logic to check if the block is allocatable before doing the
split, a physical Intel platform was seen to boot >200ms faster.
Before this change, on the platform tested, each GCD allocation
was observed to have an average of 54 blocks split and remerged
before it found a block that was actually allocatable. This
equated to 2.8 billion ticks spent doing splits and merges
vs 200 million ticks spent doing splits and merges after the
change.

This commit changes the bottom up and top down allocations to
check if the block is allocatable before splitting it.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
Currently, we use the linked list allocator crate to manage
allocations that don't hit our fixed size blocks. However,
our use of the crate is fairly minimal and it prevents us
from unmapping the pages that the allocator owns, which is
contrary to our use after free catching goals.

This commit does a large refactor to the allocator system
to stop using the linked list allocator crate and instead
back all heap allocations greater than our largest FSB with
pages directly. It then updates the management of retired and
reclaimed pages to account for the new management. To keep this
management simple, when a page is reclaimed, it is eagerly split
into one block size.

Following this change, all unused heap pages are unmapped, except
for the pages that keep metadata on this heap region. Heap pages
are retired and reclaimed in FIFO order (as are FSBs) to have
the longest time between using a page and it getting used again
and increasing the change that page will be retired and catch
any use after free.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
An earlier refactor the FSB introduced the
page accounting node as a last used cache for
heap owned regions as a potential performance
improvement. However, the performance of just
this piece was not tested independently.

When testing independently on physical HW, it
was not observed to add any performance benefit,
so it is dropped.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
In order to minimize the runtime memory footprint of
large heap allocations (greater than one page) for
architectures with large runtime allocation granularity,
e.g. 64KB for ARM64, this changes the FSB to have a
new allocation tier that will attempt allocations
between 4KB and 64KB from the retired pages it holds
instead of directly from the GCD. Allocations larger
than 64KB will still be directly allocated from the
GCD.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
@os-d
os-d force-pushed the drop_linked_list_allocator branch from 9de42b2 to f340ad7 Compare November 4, 2026 18:11
CI started enforcing a new lint on changed files and this
was flagged for using the incorrect unordered list style.

Signed-off-by: Oliver Smith-Denny <osde@microsoft.com>
@os-d
os-d enabled auto-merge (rebase) November 4, 2026 18:38
@os-d
os-d merged commit 40783df into OpenDevicePartnership:main Nov 4, 2026
11 checks passed
@os-d
os-d deleted the drop_linked_list_allocator branch November 4, 2026 19:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

impact:security Has a security impact

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Consider Dropping the Fallback Allocator [Feature]: Unmap Unused Allocator Pages

4 participants